<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  <style>
      #box{
        width: 50px;
        height: 50px;
        left: 100px;
        top: 100px;
        position: absolute;
        background-color: chartreuse;
      }
  </style>
</head>
<body>
    <div id="box"></div>
    <script>
        var left1=100;
        var top1=100;
        document.onkeydown=function(event){
           var e=event||window.event;
           switch(e.keyCode){
            case 37:left1-=5;break;
            case 38:top1-=5;break;
            case 39:left1+=5;break;
            case 40:top1+=5;break;
            default:;
           }
           box.style.left=left1+"px";
           box.style.top =top1+"px";
        }
    </script>
</body>
</html>